<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="电话号码" prop="phoneNum">
        <el-input v-model="queryParams.phoneNum" placeholder="请输入电话号码" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
<!--        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"-->
<!--                   v-hasPermi="['module:paySlip:export']">导出-->
<!--        </el-button>-->
        <el-button type="warning" icon="el-icon-download" size="mini" @click="daoru()"
                   v-hasPermi="['module:paySlip:export']">导入
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="slipList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id" v-if="false"/>
      <el-table-column label="所属时间" align="center" prop="basicSalary" width="90px">
        <template slot-scope="scope">
          {{scope.row.yearNum + "年"+scope.row.monthNum + "月"}}
        </template>
      </el-table-column>
      <el-table-column label="微信昵称" align="center" prop="wxUserName" width="90px"/>
      <el-table-column label="系统昵称" align="center" prop="sysUserName" width="90px"/>
      <el-table-column label="真实姓名" align="center" prop="realName" width="90px"/>
      <el-table-column label="电话号码" align="center" prop="phoneNum" width="120px"/>
      <el-table-column label="所属工种" align="center" prop="workType" width="90px"/>
      <el-table-column label="基本工资" align="center" prop="basicSalary" width="90px">
        <template slot-scope="scope">
          {{scope.row.basicSalary + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="绩效工资" align="center" prop="meritsSalary" width="90px">
        <template slot-scope="scope">
          {{scope.row.meritsSalary + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="其他补贴" align="center" prop="otherSubsidies" width="90px">
        <template slot-scope="scope">
          {{scope.row.otherSubsidies + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="加班费用" align="center" prop="overtimeSalary" width="90px">
        <template slot-scope="scope">
          {{scope.row.overtimeSalary + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="夜班工资" align="center" prop="nightShiftSalary" width="90px">
        <template slot-scope="scope">
          {{scope.row.nightShiftSalary + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="证书补助" align="center" prop="certificateSubsidy" width="90px">
        <template slot-scope="scope">
          {{scope.row.certificateSubsidy + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="出勤天数" align="center" prop="attendance" width="90px">
        <template slot-scope="scope">
          {{scope.row.attendance + "天"}}
        </template>
      </el-table-column>
      <el-table-column label="事假天数" align="center" prop="absence" width="90px">
        <template slot-scope="scope">
          {{scope.row.absence + "天"}}
        </template>
      </el-table-column>
      <el-table-column label="事假扣款" align="center" prop="absenceDeduction" width="90px">
        <template slot-scope="scope">
          {{scope.row.absenceDeduction + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="个人保险" align="center" prop="insurance" width="90px">
        <template slot-scope="scope">
          {{scope.row.insurance + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="个税扣款" align="center" prop="tax" width="90px">
        <template slot-scope="scope">
          {{scope.row.tax + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="其他扣款" align="center" prop="otherDeduction" width="90px">
        <template slot-scope="scope">
          {{scope.row.otherDeduction + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="工装扣款" align="center" prop="clothesDeduction" width="90px">
        <template slot-scope="scope">
          {{scope.row.clothesDeduction + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="累计扣款" align="center" prop="totalDeduction" width="100px">
        <template slot-scope="scope">
          {{scope.row.totalDeduction + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="实发工资" align="center" prop="netSalary" width="100px">
        <template slot-scope="scope">
          {{scope.row.netSalary + "元"}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="90px" fixed="right" >
        <template slot-scope="scope">
          <el-popover v-if="scope.row.state===1" placement="right" width="300" trigger="hover">
            <el-image style="width: 280px; height: 200px" :src="'https://www.caihengyun.com/chyimg/'+scope.row.signImgUrl"  fit="fill" />
            <el-tag style="cursor: pointer" :type="'success'" slot="reference">已签字</el-tag>
          </el-popover>
          <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="''">未签字</el-tag>
          <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="'danger'">取消签字</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100px" fixed="right">
        <template slot-scope="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"-->
<!--                     v-hasPermi="['module:paySlip:edit']">修改-->
<!--          </el-button>-->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:paySlip:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 工资导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!--          <div class="el-upload__tip" slot="tip">-->
          <!--            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据-->
          <!--          </div>-->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listSlip, getSlip, delSlip, addSlip, updateSlip, exportSlip} from "@/api/module/estate/slip";
import {getToken} from "@/utils/auth";
import Template from "./wyfPay";

export default {
  name: "Slip",
  components: {Template},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工资条表格数据
      slipList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 工资单导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "工资单数据导入",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/module/paySlip/importData"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询工资条列表 */
    getList() {
      this.loading = true;
      listSlip(this.queryParams).then(response => {
        this.slipList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    daoru() {
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    importTemplate() {
      window.location.href = "https://www.caihengyun.com/chyimg/file/temp/%E5%B7%A5%E8%B5%84%E6%9D%A1%E5%AF%BC%E5%85%A5%E6%A8%A1%E6%9D%BF.xlsx";
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        realName: undefined,
        phoneNum: undefined,
        workType: undefined,
        basicSalary: undefined,
        meritsSalary: undefined,
        otherSubsidies: undefined,
        insuranceSubsidy: undefined,
        overtimeSalary: undefined,
        nightShiftSalary: undefined,
        certificateSubsidy: undefined,
        attendance: undefined,
        absence: undefined,
        absenceDeduction: undefined,
        insurance: undefined,
        tax: undefined,
        otherDeduction: undefined,
        clothesDeduction: undefined,
        totalDeduction: undefined,
        netSalary: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工资条";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getSlip(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工资条";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateSlip(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSlip(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除工资条编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delSlip(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有工资条数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportSlip(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
